package com.example.final_project.controller;

import com.example.final_project.entity.Book;
import com.example.final_project.service.BookService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "图书管理", description = "图书相关的API接口")
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @Operation(summary = "图书列表页面", description = "返回图书列表页面")
    @GetMapping("/list")
    public String list(Model model,
                      @Parameter(description = "图书名称") @RequestParam(required = false) String name,
                      @Parameter(description = "ISBN") @RequestParam(required = false) String isbn,
                      @Parameter(description = "作者") @RequestParam(required = false) String author,
                      @Parameter(description = "出版社ID") @RequestParam(required = false) Long publisherId,
                      @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                      @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Book> pageInfo = bookService.getList(name, isbn, author, publisherId, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name", name);
        model.addAttribute("isbn", isbn);
        model.addAttribute("author", author);
        model.addAttribute("publisherId", publisherId);
        return "book/list";
    }

    @Operation(summary = "添加图书页面", description = "返回添加图书页面")
    @GetMapping("/add")
    public String add() {
        return "book/add";
    }

    @Operation(summary = "添加图书", description = "添加新图书")
    @ApiResponse(responseCode = "200", description = "添加成功返回success，失败返回error")
    @PostMapping("/add")
    @ResponseBody
    public String add(@Parameter(description = "图书信息") @RequestBody Book book) {
        return bookService.add(book) ? "success" : "error";
    }

    @Operation(summary = "编辑图书页面", description = "返回编辑图书页面")
    @GetMapping("/edit/{id}")
    public String edit(@Parameter(description = "图书ID") @PathVariable Long id, Model model) {
        Book book = bookService.getById(id);
        model.addAttribute("book", book);
        return "book/edit";
    }

    @Operation(summary = "更新图书", description = "更新图书信息")
    @ApiResponse(responseCode = "200", description = "更新成功返回success，失败返回error")
    @PostMapping("/edit")
    @ResponseBody
    public String edit(@Parameter(description = "图书信息") @RequestBody Book book) {
        return bookService.update(book) ? "success" : "error";
    }

    @Operation(summary = "删除图书", description = "根据图书ID删除图书")
    @ApiResponse(responseCode = "200", description = "删除成功返回success，失败返回error")
    @PostMapping("/delete/{id}")
    @ResponseBody
    public String delete(@Parameter(description = "图书ID") @PathVariable Long id) {
        return bookService.delete(id) ? "success" : "error";
    }

    @Operation(summary = "获取图书详情", description = "根据图书ID获取图书详细信息")
    @ApiResponse(responseCode = "200", description = "成功获取图书详情")
    @GetMapping("/detail/{id}")
    @ResponseBody
    public Book detailApi(@Parameter(description = "图书ID") @PathVariable Long id) {
        return bookService.getById(id);
    }

    @Operation(summary = "图书详情页面", description = "返回图书详情页面")
    @GetMapping("/detailPage/{id}")
    public String detailPage(@Parameter(description = "图书ID") @PathVariable Long id, Model model) {
        Book book = bookService.getById(id);
        model.addAttribute("book", book);
        return "book/detail";
    }

    @Operation(summary = "获取图书列表", description = "分页获取图书列表数据")
    @ApiResponse(responseCode = "200", description = "成功获取图书列表")
    @GetMapping("/api/list")
    @ResponseBody
    public Map<String, Object> apiList(
            @Parameter(description = "图书名称") @RequestParam(required = false) String name,
            @Parameter(description = "ISBN") @RequestParam(required = false) String isbn,
            @Parameter(description = "作者") @RequestParam(required = false) String author,
            @Parameter(description = "出版社ID") @RequestParam(required = false) Long publisherId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Book> pageInfo = bookService.getList(name, isbn, author, publisherId, pageNum, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return result;
    }

    @Operation(summary = "获取所有图书", description = "获取所有图书列表，不分页")
    @ApiResponse(responseCode = "200", description = "成功获取所有图书列表")
    @GetMapping("/api/all")
    @ResponseBody
    public List<Book> getAll() {
        return bookService.getAll();
    }
} 